package com.ydlclass.compress;

import com.ydlclass.compress.impl.GzipCompressor;
import com.ydlclass.config.ObjectWrapper;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class CompressorFactory {
    private final static Map<String, ObjectWrapper<Compressor>> COMPRESSOR_CACHE = new ConcurrentHashMap<>(8);
    private final static Map<Byte,ObjectWrapper<Compressor>> COMPRESSOR_CACHE_CODE = new ConcurrentHashMap<>(8);
    static{
        ObjectWrapper<Compressor> gzip = new ObjectWrapper<>((byte) 1, "gzip", new GzipCompressor());

        COMPRESSOR_CACHE.put("gzip",gzip);

        COMPRESSOR_CACHE_CODE.put((byte) 1,gzip);

    }

    /**
     * 使用工厂方法 获得一个 CompressorWrapper
     * @param compressorType 序列化类型
     * @return CompressorWrapper
     */
    public static ObjectWrapper<Compressor> getCompressor(String compressorType) {
        ObjectWrapper<Compressor> compressorObjectWrapper = COMPRESSOR_CACHE.get(compressorType);
        if(compressorObjectWrapper == null){
            log.error("未找到你配置的{}压缩算法，默认选用gzip算法。",compressorObjectWrapper);
            return COMPRESSOR_CACHE.get("gzip");
        }
        return compressorObjectWrapper;
    }

    public static ObjectWrapper<Compressor> getCompressor(byte compressorCode) {
        ObjectWrapper<Compressor> compressorObjectWrapper = COMPRESSOR_CACHE_CODE.get(compressorCode);
        if(compressorObjectWrapper == null){
            log.error("未找到你配置的编号{}压缩算法，默认选用gzip算法。",compressorCode);
            return COMPRESSOR_CACHE_CODE.get((byte) 1);
        }
        return compressorObjectWrapper;
    }

    /**
     * 给工厂中新增一个压缩方式
     * @param compressorObjectWrapper 压缩类型的包装
     */
    public static void addCompressor(ObjectWrapper<Compressor> compressorObjectWrapper){
        COMPRESSOR_CACHE.put(compressorObjectWrapper.getName(),compressorObjectWrapper);
        COMPRESSOR_CACHE_CODE.put(compressorObjectWrapper.getCode(),compressorObjectWrapper);
    }

}
